package com.sys.controller.monitor;

import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.core.page.TableDataInfo;
import com.sys.common.enums.BusinessType;
import com.sys.common.utils.poi.ExcelUtil;
import com.sys.framework.web.service.SysPasswordService;
import com.sys.system.domain.SysLoginInfo;
import com.sys.system.service.ISysLoginInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author LGY
 */
@Tag(name = "登录日志API")
@RestController
@RequestMapping("/monitor/loginInfo")
public class SysLoginInfoController extends BaseController {

    @Autowired
    private ISysLoginInfoService loginInfoService;
    @Autowired
    private SysPasswordService passwordService;

    @Operation(summary = "登录日志分页列表")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysLoginInfo logininfor) {
        startPage(logininfor);
        List<SysLoginInfo> list = loginInfoService.selectLogininforList(logininfor);
        return getDataTable(list);
    }

    @Operation(summary = "登录日志导出")
    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfo logininfor) {
        List<SysLoginInfo> list = loginInfoService.selectLogininforList(logininfor);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @Operation(summary = "登录日志删除")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public AjaxResult remove(@PathVariable String[] infoIds) {
        loginInfoService.deleteLogininforByIds(infoIds);
        return AjaxResult.success();
    }

    @Operation(summary = "登录日志清空")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        loginInfoService.cleanLogininfor();
        return success();
    }

    @Operation(summary = "账户解锁")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public AjaxResult unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
}
